import React from 'react'
import { Switch, Route, Redirect } from 'react-router-dom'

export default function RouterView(props) {
    let routes = props.routes.filter(item => item.component)
    let redirect = props.routes.filter(item => item.redirect)

    return (
        <Switch>
            {
                routes.map(item => {
                    return <Route key={item.path} path={item.path} render={props => {
                        if (item.children) {
                            return <item.component routes={item.children} {...props} />
                        }
                        return <item.component {...props} />
                    }} />
                })
            }
            {
                redirect.map(item => {
                    return <Redirect key={item.path} from={item.path} to={item.redirect} />
                })
            }
        </Switch>
    )
}
